package beanLifeCycle;

import java.beans.PropertyDescriptor;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;

public class MyInstantitationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {

	//实例化Bean 之前调用
	@Override
	public Object postProcessBeforeInstantiation(Class beanClass, String beanName) throws BeansException {
		if("car".equals(beanName)) {
			System.out.println("调用 InstantiationAwareBeanPostProcessorAdapter.postProcessBeforeInstantiation()");
		}
//		return super.postProcessBeforeInstantiation(beanClass, beanName);
		return null;
	}

	//实例化bean之后调用
	@Override
	public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
		if("car".equals(beanName)) {
			System.out.println("调用 InstantiationAwareBeanPostProcessorAdapter.postProcessAfterInstantiation()");
		}
//		return super.postProcessAfterInstantiation(bean, beanName);
		return true;
	}

	//在设置某个属性时调用
	@Override
	public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)
		throws BeansException {
		if("car".equals(beanName)) {
			Car car = (Car)bean;
			car.setBrand("TestCar");
			System.out.println("调用 InstantiationAwareBeanPostProcessorAdapter.postProcessPropertyValues()");
		}
//		return super.postProcessPropertyValues(pvs, pds, bean, s);
		return pvs;
	}

}
